#include <string.h>
#include <unistd.h>
#include <stdio.h>

static int num = 0;
static char namebuf[20];
static char prefix[] = "/tmp/tmp";

itoa(int i, char* string)
{
    int power, j;
    j = i;
    for(power=1; j >= 1; j/=10) {
        power *= 10;
    }
    for(;power>0;power/=10) {
        *string++ = '0' + i/power;
        i %= power;
    }
    *string = '\0'; // 设置char数组的结尾为'\0'
}

char* gentemp()
{
    int length, pid;

    // 获得进程标识符
    pid = getpid();

    strcpy(namebuf, prefix);
    length = strlen(namebuf);

    // 在文件名中增加pid部分
    itoa(pid, &namebuf[length]);

    // printf("%s", namebuf);
    
    strcat(namebuf, ".");

    length = strlen(namebuf);

    do
    {
        // 增加后缀
        itoa(num++, &namebuf[length]);

        printf("%s", namebuf);

    } while (access(namebuf, 0) != -1);

    return namebuf;
    
}



int main()
{
    gentemp();
}